
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.bo.IEmpHistOrgBo;
import com.wyzz.hr.dao.IEmpHistOrgDao;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.EmpHistOrg;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;


public class EmpHistOrgBoImpl implements IEmpHistOrgBo {

	public EmpHistOrgBoImpl() {
	}

	public IEmpHistOrgDao getEmpHistOrgDao() {
		return empHistOrgDao;
	}

	public void setEmpHistOrgDao(IEmpHistOrgDao empHistOrgDao) {
		this.empHistOrgDao = empHistOrgDao;
	}

	public int getEhosNum(Date queryDate, String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.EmpHistOrg.class);
		dc.add(Restrictions.eq(EmpHistOrg.PROP_EHO_DEPT_NO, new Department(
				deptId)));
		dc.add(Restrictions.le(EmpHistOrg.PROP_EHO_VALID_FROM, queryDate));
		dc.add(Restrictions.or(Restrictions.ge(EmpHistOrg.PROP_EHO_VALID_TO,
				queryDate), Restrictions.isNull(EmpHistOrg.PROP_EHO_VALID_TO)));
		return empHistOrgDao.findByCriteria(dc).size();
	}

	public Employee getDeptHead(Date queryDate, String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.EmpHistOrg.class);
		dc.setFetchMode(EmpHistOrg.PROP_EHO_EMP_NO, FetchMode.JOIN);
		dc.add(Restrictions.eq(EmpHistOrg.PROP_EHO_PB_NO,
				new PositionBase(pbId)));
		dc.add(Restrictions.le(EmpHistOrg.PROP_EHO_VALID_FROM, queryDate));
		dc.add(Restrictions.or(Restrictions.ge(EmpHistOrg.PROP_EHO_VALID_TO,
				queryDate), Restrictions.isNull(EmpHistOrg.PROP_EHO_VALID_TO)));
		List ehoList = empHistOrgDao.findByCriteria(dc);
		if (ehoList != null && ehoList.size() == 1)
			return ((EmpHistOrg) ehoList.get(0)).getEhoEmpNo();
		else
			return null;
	}

	public Map getEmphistorgOfDept(List deptList) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.EmpHistOrg.class);
		dc.add(Restrictions.in(EmpHistOrg.PROP_EHO_DEPT_NO, deptList));
		dc.add(Restrictions.isNull(EmpHistOrg.PROP_EHO_VALID_TO));
		List ehoList = empHistOrgDao.findByCriteria(dc);
		Map emphistorgMap = new HashMap();
		EmpHistOrg histOrg;
		for (Iterator i$ = ehoList.iterator(); i$.hasNext(); emphistorgMap.put(
				histOrg.getEhoEmpNo().getId(), histOrg))
			histOrg = (EmpHistOrg) i$.next();

		return emphistorgMap;
	}

	public EmpHistOrg getLatestEmphistorg(Employee emp) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.EmpHistOrg.class);
		dc.add(Restrictions.eq(EmpHistOrg.PROP_EHO_EMP_NO, emp));
		dc.add(Restrictions.isNull(EmpHistOrg.PROP_EHO_VALID_TO));
		List ehoList = empHistOrgDao.findByCriteria(dc);
		if (ehoList != null && ehoList.size() > 0)
			return (EmpHistOrg) ehoList.get(0);
		else
			return null;
	}

	public List exeHqlList(String hql) {
		return empHistOrgDao.exeHqlList(hql);
	}

	public boolean isExistPb(String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.EmpHistOrg.class);
		dc.add(Restrictions.eq(EmpHistOrg.PROP_EHO_PB_NO,
				new PositionBase(pbId)));
		List ehoList = empHistOrgDao.findByCriteria(dc);
		return ehoList != null && ehoList.size() != 0;
	}

	public void saveEmpHistOrg(Employee emp, Department newDept,
			PositionBase newPB) {
		IEmpHistOrgBo emphistorgBo = (IEmpHistOrgBo) SpringBeanFactory
				.getBean("empHistOrgBo");
		EmpHistOrg latestHistOrg = emphistorgBo.getLatestEmphistorg(emp);
		if (latestHistOrg != null && latestHistOrg.getEhoValidTo() == null) {
			latestHistOrg.setEhoValidTo(new Date());
			latestHistOrg.setEhoIsLatest(new Integer(0));
		}
		EmpHistOrg newHistOrg = new EmpHistOrg(null, emp,
				newDept != null ? newDept : emp.getEmpDeptNo(),
				newPB != null ? newPB : emp.getEmpPbNo(), new Date(),
				new Integer(1));
		if (latestHistOrg != null)
			empHistOrgDao.saveOrupdate(latestHistOrg);
		if (newHistOrg != null)
			empHistOrgDao.saveOrupdate(newHistOrg);
	}

	public void saveLatestEmpHistOrg(Employee emp, Department newDept,
			PositionBase newPB) {
		IEmpHistOrgBo emphistorgBo = (IEmpHistOrgBo) SpringBeanFactory
				.getBean("empHistOrgBo");
		EmpHistOrg latestHistOrg = emphistorgBo.getLatestEmphistorg(emp);
		if (latestHistOrg != null) {
			latestHistOrg.setEhoDeptNo(newDept);
			latestHistOrg.setEhoPbNo(newPB);
			empHistOrgDao.saveOrupdate(latestHistOrg);
		} else {
			EmpHistOrg newHistOrg = new EmpHistOrg(null, emp,
					newDept != null ? newDept : emp.getEmpDeptNo(),
					newPB != null ? newPB : emp.getEmpPbNo(), new Date(),
					new Integer(1));
			empHistOrgDao.saveOrupdate(newHistOrg);
		}
	}

	public List gernerateEmpHistOrg(Employee emp, Department newDept,
			PositionBase newPB, Date date) {
		IEmpHistOrgBo emphistorgBo = (IEmpHistOrgBo) SpringBeanFactory
				.getBean("empHistOrgBo");
		EmpHistOrg latestHistOrg = emphistorgBo.getLatestEmphistorg(emp);
		if (latestHistOrg != null && latestHistOrg.getEhoValidTo() == null) {
			latestHistOrg.setEhoValidTo(date != null ? date : new Date());
			latestHistOrg.setEhoIsLatest(new Integer(0));
		}
		EmpHistOrg newHistOrg = new EmpHistOrg(null, emp,
				newDept != null ? newDept : emp.getEmpDeptNo(),
				newPB != null ? newPB : emp.getEmpPbNo(), new Date(),
				new Integer(1));
		List list = new ArrayList();
		if (latestHistOrg != null)
			list.add(latestHistOrg);
		if (newHistOrg != null)
			list.add(newHistOrg);
		return list;
	}

	private IEmpHistOrgDao empHistOrgDao;
}
